package game.ouroboros.magic;

import java.util.List;
import javax.microedition.khronos.opengles.GL10;
import android.util.Log;
import framework.graphics.opengl.Animation;
import framework.graphics.opengl.Camera2D;
import framework.graphics.opengl.GLGraphics;
import framework.graphics.opengl.SpriteBatcher;
import framework.graphics.opengl.TextureRegion;
import game.ouroboros.Assets;
import game.ouroboros.enemy.*;
import game.ouroboros.temp.GenericPersonagem;
import game.ouroboros.temp.GenericTrap;
import game.ouroboros.traps.Portao;
import game.ouroboros.traps.*;

public class WorldRenderer {
	static final float FRUSTUM_WIDTH = 10;
	static final float FRUSTUM_HEIGHT = 15;
	GLGraphics glGraphics;
	World world;
	Camera2D cam;
	SpriteBatcher batcher;

	public WorldRenderer(GLGraphics glGraphics, SpriteBatcher batcher,
			World world) {
		this.glGraphics = glGraphics;
		this.world = world;
		this.cam = new Camera2D(glGraphics, 320, 480);
		this.batcher = batcher;
	}

	public void render() {

		Log.v("<WORLD>", "X: " + cam.position.x + " Y: " + cam.position.y);
		cam.zoom = 1;

		cam.setViewportAndMatrices();
		renderBackground();
		renderObjects();
	}

	public void renderTrapsOnlyNow() {
		batcher.beginBatch(Assets.inimigos);

		int len = world.traps.size();
		if (len == 0)
			return;
		TextureRegion keyFrame = null;
		for (int i = 0; i < len; i++) {
			Trap p = world.traps.get(i);
			if (p instanceof Portao) {
				Log.v("<RENDER>", "PORTAL at: " + p.position.x + " "
						+ p.position.y);
				batcher.drawExactSprite(p.position.x, p.position.y,
						p.dimension.x, p.dimension.y, Assets.portal);
			} else {
				if (p instanceof ElectricArc) {
					keyFrame = Assets.ELECTRIC_ARC;
				} else if (p instanceof EnergyField) {
					keyFrame = Assets.ENERGY_FIELD;
				} else if (p instanceof Estalactites) {
					keyFrame = Assets.ESTALACTITES;
				} else if (p instanceof FlameThrower) {
					keyFrame = Assets.FLAME_THROWER;
				} else if (p instanceof Geiser) {
					keyFrame = Assets.GEISER;
				} else if (p instanceof LavaDig) {
					keyFrame = Assets.LAVA_DIG;
				} else if (p instanceof SpikeBarrier) {
					keyFrame = Assets.SPIKE_BARRIER;
				} else if (p instanceof Portao) {
					keyFrame = Assets.portal;
				} else if (p instanceof Tornado) {
					keyFrame = Assets.TORNADO;
				} else if (p instanceof WaterShooter) {
					keyFrame = Assets.WATER_SHOOTER;
				}

				else if (p instanceof WaterSlide) {
					keyFrame = Assets.WATER_SLIDE;
				}

				else if (p instanceof WindBreaker) {
					keyFrame = Assets.WIND_BREAKER;
				}
				batcher.drawExactSprite(p.position.x, p.position.y,
						p.dimension.x*3, p.dimension.y*3, keyFrame);
			}
		}
		batcher.endBatch();
	}

	public void renderBackground() {
		batcher.beginBatch(Assets.background);
		batcher.drawExactSprite(0, 0, 320, 480, Assets.backgroundRegion);
		batcher.endBatch();
	}

	public void renderObjects() {
		GL10 gl = glGraphics.getGL();
		gl.glEnable(GL10.GL_BLEND);
		gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);

		batcher.beginBatch(Assets.inimigos);
		renderTraps();
		renderPersonagens();

		batcher.endBatch();
		gl.glDisable(GL10.GL_BLEND);
		gl.glDisable(GL10.GL_BLEND);
	}

	private void renderPersonagens() {
		Log.v("<RENDER>", "PERSONAGENS");
		List<Enemy> pL = world.level.getAlivePersonagens();
		int len = pL.size();
		if (len == 0)
			return;
		TextureRegion keyFrame = null;
		for (int i = 0; i < len; i++) {
			Enemy enemy = pL.get(i);
			if (enemy instanceof AirSkiper) {
				keyFrame = Assets.AIR_SKIPER;
			} else if (enemy instanceof Skiper) {
				keyFrame = Assets.SKIPER;
			} else if (enemy instanceof ArmoredOgre) {
				keyFrame = Assets.ARMORED_OGRE;
			} else if (enemy instanceof Boom) {
				keyFrame = Assets.BOOM;
			} else if (enemy instanceof Chaman) {
				keyFrame = Assets.CHAMAN;
			} else if (enemy instanceof DarkOgre) {
				keyFrame = Assets.DARK_OGRE;
			} else if (enemy instanceof Dragon) {
				keyFrame = Assets.DRAGON;
			} else if (enemy instanceof EkChua) {
				keyFrame = Assets.EK_CHUA;
			} else if (enemy instanceof FireArcher) {
				keyFrame = Assets.FIRE_ARCHER;
			} else if (enemy instanceof FireOgre) {
				keyFrame = Assets.FIRE_OGRE;
			} else if (enemy instanceof IceArcher) {
				keyFrame = Assets.ICE_ARCHER;
			} else if (enemy instanceof Ixtab) {
				keyFrame = Assets.IXTAB;
			} else if (enemy instanceof NightMare) {
				keyFrame = Assets.NIGHTMARE;
			} else if (enemy instanceof Ogre) {
				keyFrame = Assets.OGRE;
			}
			Log.v("<RENDER>", "PERSONAGEM");
			batcher.drawSprite(enemy.position.x, enemy.position.y,
					enemy.dimension.x*2, enemy.dimension.y*2, keyFrame);
		}

	}

	private void renderTraps() {
		int len = world.traps.size();
		TextureRegion keyFrame = null;
		for (int i = 0; i < len; i++) {
			Trap p = world.traps.get(i);
			if (p instanceof Portao) {
				Log.v("<RENDER>", "PORTAL at: " + p.position.x + " "
						+ p.position.y);
				batcher.drawExactSprite(p.position.x, p.position.y,
						p.dimension.x, p.dimension.y, Assets.portal);
			} else {
				if (p instanceof ElectricArc) {
					keyFrame = Assets.ELECTRIC_ARC;
				} else if (p instanceof EnergyField) {
					keyFrame = Assets.ENERGY_FIELD;
				} else if (p instanceof Estalactites) {
					keyFrame = Assets.ESTALACTITES;
				} else if (p instanceof FlameThrower) {
					keyFrame = Assets.FLAME_THROWER;
				} else if (p instanceof Geiser) {
					keyFrame = Assets.GEISER;
				} else if (p instanceof LavaDig) {
					keyFrame = Assets.LAVA_DIG;
				} else if (p instanceof SpikeBarrier) {
					keyFrame = Assets.SPIKE_BARRIER;
				} else if (p instanceof Portao) {
					keyFrame = Assets.portal;
				} else if (p instanceof Tornado) {
					keyFrame = Assets.TORNADO;
				} else if (p instanceof WaterShooter) {
					keyFrame = Assets.WATER_SHOOTER;
				}

				else if (p instanceof WaterSlide) {
					keyFrame = Assets.WATER_SLIDE;
				}

				else if (p instanceof WindBreaker) {
					keyFrame = Assets.WIND_BREAKER;
				}
				batcher.drawExactSprite(p.position.x, p.position.y,
						p.dimension.x*2, p.dimension.y*2, keyFrame);
			}
		}
	}

}
